﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Net;
using System.ComponentModel;
using System.Threading;
using System.Timers;

namespace sabConn
{
    public class ServerConnection
    {
        //User input information.
        private string _server;
        public string server { get { return _server; } }
        private string _port;
        public string port { get { return _port; } }
        private string _apikey;
        public string apikey { get { return _apikey; } }
        private string _username;
        public string username { get { return _username; } }
        private string _password;
        public string password { get { return _password; } }
        private double _refreshinterval;
        public double refreshinterval { get { return _refreshinterval; } }

        //Internal variables
        internal string serverstring;
        internal static readonly object qlocker = new object();
        internal static readonly object hlocker = new object();


        //Classes
        private Queue _queue;
        public Queue queue { get { lock(qlocker) { return _queue; }} }
        private History _history;
        public History history { get { lock(hlocker) { return _history; } } }

        public ServerConnection(string server, string port, string apikey, string username="", string password="", double refreshinterval=2000)
        {
            serverstring = "http://" + server + ":" + port;
            _server = server;
            _port = port;
            _apikey = apikey;
            _username = username;
            _password = password;
            _refreshinterval = refreshinterval;

            _queue = new Queue(this);
            _history = new History(this);
        }

        public void update()
        {
            try
            {
                queue.update();
                history.update();
            }
            catch
            {

            }
        }

        internal void requestpage(string url)
        {
            //tbReply.Clear();
            try
            {
                HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(url);
                // *** Set properties
                loHttp.Timeout = 10000;     // 10 secs
                loHttp.UserAgent = "Sabnzbd Client";
                // *** Retrieve request info headers
                HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse();
                Encoding enc = Encoding.GetEncoding(1252);  // Windows default Code Page
                StreamReader loResponseStream =
                new StreamReader(loWebResponse.GetResponseStream(), enc);
                string lcHtml = loResponseStream.ReadToEnd();
                //tbReply.Text = lcHtml.Replace("\n", "");
                loWebResponse.Close();
                loResponseStream.Close();
            }
            catch
            {
                //tbReply.Text = "Working..";
            }
        }

    }
}
